package com.heartbeat.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.transform.TransformerException;

import org.apache.commons.lang.StringUtils;
import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

import com.sun.org.apache.xpath.internal.XPathAPI;

public class Parser {
	
	public Document getDocument(String webUrl, String codeType){
		DOMParser parser = new DOMParser();
		
		BufferedReader in = null;
		URLConnection con = null;
		Document doc = null;
		try {
			//con = ConnectionProxy.getInstance().getConnection(webUrl);
			URL url = new URL(StringUtils.trim(webUrl));

			con = url.openConnection();
			con.setRequestProperty(
	                "User-Agent",
	                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
			con.setRequestProperty("Content-Type", "text/html; charset=utf-8");
			con.setReadTimeout(10000);
			
			
			parser.setProperty("http://cyberneko.org/html/properties/default-encoding", codeType);
			parser.setFeature("http://xml.org/sax/features/namespaces", false);
			in = new BufferedReader(new InputStreamReader(con.getInputStream(), codeType));
			parser.parse(new InputSource(in));
		
			doc = parser.getDocument();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXNotRecognizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return doc;
	}
	
	public NodeList getNodeList(Document doc, String xpath){
		try {
			NodeList nodeList = XPathAPI.selectNodeList(doc, xpath);
			return nodeList;
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	protected static String dowith(String s) {

		byte[] b = s.getBytes();
		if (b.length > 0) {
			for (int i = 0; i < b.length; i++) {
				if ((int) b[i] == 63) {
					b[i] = (byte) '\u0020';
				}
			}
		}
		return new String(b).trim();
	}

	public static void main(String args[]){
		String xpath = "//HTML//BODY//DIV[@class='content']//DIV[@class='rights']//DIV[@class='block3']//UL[@class='booksum pd10 pd8 pd6 tl']//LI";
		String imgXpath="//HTML//BODY//DIV[@class='content']//DIV[@class='rights']//DIV[@class='block']//DIV[@class='ClBookText']//DIV[@class='bookimg fl']//IMG[@class='img']";
		
		String webUrl="http://www.16kbook.org/Book/13054/Index.aspx";
		String codeType="gb2312";
		
		Parser parser = new Parser();
		Document doc = parser.getDocument(webUrl, codeType);
		NodeList nodeList = parser.getNodeList(doc, xpath);
		
		if(nodeList != null && nodeList.getLength()>0){
			Node node = nodeList.item(0);
			//System.out.println(dowith(node.getTextContent()));
			System.out.println(node.getTextContent());
		}
		
		NodeList imgNodeList = parser.getNodeList(doc, imgXpath);
		if(nodeList != null && nodeList.getLength()>0){
			Node node = imgNodeList.item(0);
			//System.out.println(dowith(node.getTextContent()));
			System.out.println(node.getAttributes().getNamedItem("src").getNodeValue());
		}
	}
}
